﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using com.eforceglobal.crux.DTOs;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.DTOs.Configuration;

public partial class Administration_ManageUsers : BaseView, IManageUsers
{
    ManageUsersController _controller = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new ManageUsersController(this);
        _controller.InitView(IsPostBack);

    }

    public void grdUsers_RowCommand(object sender, CommandEventArgs e)
    {
        RowCommands rowCommand = (RowCommands)Enum.Parse(typeof(RowCommands), e.CommandName);
        string userId = e.CommandArgument.ToString();
            
        if (rowCommand == RowCommands.EditItem)
        {
            _controller.OpenUser(int.Parse(userId));
        }
        else if (rowCommand == RowCommands.DeleteItem)
        {
            _controller.DeleteUser(int.Parse(userId));
        }
        else if (rowCommand == RowCommands.OpenView)
        {
            _controller.ManageUserDetails(int.Parse(userId));
        }
        else if (rowCommand == RowCommands.OpenSecondView)
        {
            _controller.ResetUserPassword(int.Parse(userId));
        }

    }

    protected void grdUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton deleteButton = (ImageButton)e.Row.FindControl("btnItemDelete");
            string ReplaceConfirmation = UILiterals.DeleteUserConfirmation.Replace("{0}",
                DataBinder.Eval(e.Row.DataItem, "UserName").ToString());
            deleteButton.Attributes.Add("onclick", "javascript:return "
                + "confirm('" + ReplaceConfirmation + "')");
        }
    }


    #region IManageUsers Members

    public List<UserInformation> Users
    {
       
        set
        {
            grdUsers.DataSource = value;
            grdUsers.DataBind();
        }
    }

    public UserInformation CurrentUser
    {
        get
        {
            UserInformation userInformation = new UserInformation();
            userInformation.UserID = int.Parse(lblUserIDValue.Text);
            userInformation.UserName = txtUserName.Text;
            userInformation.FirstName = txtFirstName.Text;
            userInformation.MiddleName = txtMiddleName.Text;
            userInformation.LastName = txtLastName.Text;
            userInformation.UserComments = txtUserComments.Text;
            userInformation.CompanyID = CompanyID;
            userInformation.CompanyName = _controller.GetCompanyName(CompanyID).CompanyName;
            return userInformation;
        }
        set
        {
            lblUserIDValue.Text = value.UserID.ToString();
            txtUserName.Text = value.UserName;
            txtFirstName.Text = value.FirstName;
            txtMiddleName.Text = value.MiddleName;
            txtLastName.Text = value.LastName;
            txtUserComments.Text = value.UserComments;
        }
    }

    public FormMode FormMode
    {
        get
        {
            if (pnlList.Visible == true)
                return FormMode.List;
            else if (pnlAddEdit.Visible == true)
                return FormMode.AddEdit;
            else
                throw UIExceptions.InvalidFormModeException;
        }
        set
        {
            if (value == FormMode.List)
            {
                pnlList.Visible = true;
                pnlAddEdit.Visible = false;
            }
            else if (value == FormMode.AddEdit)
            {
                pnlList.Visible = false;
                pnlAddEdit.Visible = true;
            }
            else
            {
                throw UIExceptions.InvalidFormModeException;
            }
        }
    }

    #endregion
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _controller.SaveUser();

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _controller.CancelAddEdit();
    }
    protected void btnAddNewUser_Click(object sender, EventArgs e)
    {
        _controller.OpenUser(0);
    }

    #region IManageUsers Members


    public ErrorInformation ErrorDetails
    {
        get
        {
            return pnlError.ErrorInformation;
        }
        set
        {
            pnlError.ErrorInformation = value;
        }
    }

    #endregion

    #region IManageUsers Members


    public int CompanyID
    {
        get { return int.Parse(Request.Params["id"].ToString()); }
    }

  
    #endregion

    
}
